package view.layers;

import java.io.IOException;
import javax.microedition.lcdui.Graphics;
import javax.microedition.lcdui.Image;
import model.structs.Point;


/**
 * The map and terrain of an area. Since the entire terrain may not always be viewed on a screen
 * due to physical limitations, the terrain's image will have to be shifted left, right, up, and
 * down as necessary.
 *
 * @author rhaq
 * @version 1.00 2009-05-06 Initial submission.
 */
public class TerrainLayer extends Layer
{
	/** Represents the image associated with this terrain. */
	private Image mapImage;

	/** The height of the screen where the output is being displayed. */
	private final int screenHeight;

	/** The width of the screen where the output is being displayed. */
	private final int screenWidth;


	/**
	 * Constructs the terrain layer with the specified dimensions.
	 * @param w The width of this layer.
	 * @param h The height of this layer.
	 * @throws IOException If the graphics for this layer cannot be loaded.
	 */
	public TerrainLayer(int w, int h) throws IOException
	{
		super(w, h);

		this.screenHeight = h;
		this.screenWidth = w;
		this.maximum = new Point();

		setMapImage( Image.createImage("/map_marcyProjects.jpg") );
	}


	/**
	 * Retrieves the image associated with this terrain.
	 * @return The image associated with this terrain.
	 */
	public Image getMapImage()
	{
		return mapImage;
	}


	/* (non-Javadoc)
	 * @see view.layers.Layer#isBottomEdged()
	 */
	public boolean isBottomEdged()
	{
		return current.getY() == minimum.getY();
	}


	/* (non-Javadoc)
	 * @see view.layers.Layer#isLeftEdged()
	 */
	public boolean isLeftEdged()
	{
		return current.getX() == maximum.getX();
	}


	/* (non-Javadoc)
	 * @see view.layers.Layer#isRightEdged()
	 */
	public boolean isRightEdged()
	{
		return current.getX() == minimum.getX();
	}


	/* (non-Javadoc)
	 * @see view.layers.Layer#isTopEdged()
	 */
	public boolean isTopEdged()
	{
		return current.getY() == maximum.getY();
	}


	/* (non-Javadoc)
	 * @see model.Layer#moveDown()
	 */
	public void moveDown()
	{
		int val = Math.min( current.getY()+screenHeight, maximum.getY() );
		current.setY(val);
	}


	/* (non-Javadoc)
	 * @see model.Layer#moveLeft()
	 */
	public void moveLeft()
	{
		int val = Math.max( current.getX()-screenWidth, minimum.getX() );
		current.setX(val);
	}


	/* (non-Javadoc)
	 * @see model.Layer#moveRight()
	 */
	public void moveRight()
	{
		int val = Math.min( current.getX()+screenWidth, maximum.getX() );
		current.setX(val);
	}


	/* (non-Javadoc)
	 * @see model.Layer#moveUp()
	 */
	public void moveUp()
	{
		int val = Math.max( current.getY()-screenHeight, minimum.getY() );
		current.setY(val);
	}


	/* (non-Javadoc)
	 * @see model.Layer#paint(javax.microedition.lcdui.Graphics)
	 */
	public void paint(Graphics g)
	{
		g.drawImage(mapImage, current.getX(), current.getY(), RELATIVE_POSITION);
	}


	/**
	 * Sets the mapImage to the specified value.
	 * @param mapImage The mapImage to set.
	 */
	public void setMapImage(Image mapImage)
	{
		this.mapImage = mapImage;
		this.minimum = new Point( screenWidth-mapImage.getWidth(), screenHeight-mapImage.getHeight() );
	}
}